package com.demo.redis.pubsub;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 参考：https://blog.csdn.net/lihao21/article/details/48370687
 * https://blog.csdn.net/u011489043/article/details/78780255
 * https://www.cnblogs.com/foundwant/p/6382083.html
 */
public class SubscriberThread extends Thread {
    private final JedisPool jedisPool;
    private final Subscriber subscriber;
    private final String channel;

    public SubscriberThread(JedisPool jedisPool, Subscriber subscriber, String channel) {
        super("subscriberThread");
        this.jedisPool = jedisPool;
        this.subscriber = subscriber;
        this.channel = channel;
    }

    @Override
    public void run() {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.subscribe(subscriber, channel);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
